// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RibbonRegionAdapter.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Enables use of a Ribbon control as a Prism region.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Common.Adapters
{
    using System.Collections.Specialized;
    using System.Windows;

    using Fluent;

    using Microsoft.Practices.Prism.Regions;

    /// <summary>
    /// Enables use of a Ribbon control as a Prism region.
    /// </summary>
    /// <remarks> See Developer's Guide to Microsoft Prism (Ver. 4), p. 189.</remarks>
    public class RibbonRegionAdapter : RegionAdapterBase<Ribbon>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RibbonRegionAdapter"/> class.
        /// </summary>
        /// <param name="behaviorFactory">
        /// Allows the registration of the default set of RegionBehaviors.
        /// </param>
        public RibbonRegionAdapter(IRegionBehaviorFactory behaviorFactory)
            : base(behaviorFactory)
        {
        }

        /// <summary>
        /// Adapts a WPF control to serve as a Prism IRegion.
        /// </summary>
        /// <param name="region">The new region being used.</param>
        /// <param name="regionTarget">The WPF control to adapt.</param>
        protected override void Adapt(IRegion region, Ribbon regionTarget)
        {
            region.Views.CollectionChanged += (sender, e) =>
            {
                switch (e.Action)
                {
                    case NotifyCollectionChangedAction.Add:
                        foreach (FrameworkElement element in e.NewItems)
                        {
                            if (element is RibbonTabItem)
                            {
                                regionTarget.Tabs.Add(element as RibbonTabItem);
                            }
                            else if (element is RibbonContextualTabGroup)
                            {
                                regionTarget.ContextualGroups.Add(element as RibbonContextualTabGroup);
                            }
                        }

                        break;

                    case NotifyCollectionChangedAction.Remove:
                        foreach (UIElement elementLoopVariable in e.OldItems)
                        {
                            var element = elementLoopVariable;

                            if (element is RibbonTabItem)
                            {
                                if (regionTarget.Tabs.Contains(element as RibbonTabItem))
                                {
                                    regionTarget.Tabs.Remove(element as RibbonTabItem);
                                }
                            }
                            else if (element is RibbonContextualTabGroup)
                            {
                                if (regionTarget.ContextualGroups.Contains(element as RibbonContextualTabGroup))
                                {
                                    regionTarget.ContextualGroups.Remove(element as RibbonContextualTabGroup);
                                }
                            }
                        }

                        break;
                }
            };
        }

        /// <summary>
        /// Creates the region.
        /// </summary>
        /// <returns>Region object</returns>
        protected override IRegion CreateRegion()
        {
            return new SingleActiveRegion();
        }
    }
}